package com.jh.netty.mqtt;


import org.eclipse.paho.mqttv5.client.*;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.MqttMessage;
import org.eclipse.paho.mqttv5.common.packet.MqttProperties;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class MqttV5 {

    public static void main(String[] args) throws MqttException {
        MqttClient client = new MqttClient(
                "tcp://mqtt.cn-zly.com:9999",
                UUID.randomUUID().toString());

        // MQTT 连接选项
        MqttConnectionOptions connOpts = new MqttConnectionOptionsBuilder()
                .username("ef1ac9cd117d437cb53a285ce6fbe22a")
                .password("bd3ecf6c64f5435b92f83553f9dee17c".getBytes(StandardCharsets.UTF_8))
                .build();
         client.setCallback(new MqttCallback() {
             @Override
             public void disconnected(MqttDisconnectResponse disconnectResponse) {
                 System.out.println(disconnectResponse.toString());
             }

             @Override
             public void mqttErrorOccurred(MqttException exception) {
                 System.out.println(exception.toString());
             }

             @Override
             public void messageArrived(String topic, MqttMessage message) throws Exception {
                 System.out.println(topic);
                 System.out.println(new String(message.getPayload()));
                 System.out.println("==========");
             }

             @Override
             public void deliveryComplete(IMqttToken token) {
                 System.out.println(token);
             }

             @Override
             public void connectComplete(boolean reconnect, String serverURI) {
                 System.out.println("=====connectComplete====" + reconnect + " === " + serverURI);
             }

             @Override
             public void authPacketArrived(int reasonCode, MqttProperties properties) {
                 System.out.println("authPacketArrived  " + reasonCode + "\n" + properties);
             }
         });
         client.connect(connOpts);

         client.subscribe("RAW/3/300051", 0);
    }
}
